﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Baimasoft.FrameWork.Base;
using Baimasoft.Manage.SqlServerDAL;

public partial class SysManage_OrgTree_Other_Edit : System.Web.UI.Page
{
    CommClass MyCl = new CommClass();
    DeptPersManage MyDPM = new DeptPersManage();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (ViewState["VOrgType"] == null || ViewState["VOrgType"].ToString().CompareTo("") == 0)
            {  //已被使用
                ViewState["VOrgType"] = "0";
            }

            if (ViewState["VParentChildID"] == null || ViewState["VParentChildID"].ToString().CompareTo("") == 0)
            {
                ViewState["VParentChildID"] = "";
            }

            try
            {
                ViewState["VOrgType"] = Request.QueryString["OrgType"].ToString();
                HOrgType.Value = ViewState["VOrgType"].ToString().Trim();
            }
            catch
            {
            }

            try
            {
                ViewState["VParentChildID"] = Request.QueryString["ParentChildID"].ToString();
                hParentChildIDCol .Value    = ViewState["VParentChildID"].ToString().Trim().Trim();
            }
            catch
            {
            }

            OrgPropBind();
            TypeBind();
            org_characterBind();
            org_LevelBind();
            org_SortBind();
            orgTypesBind();
        }
    }

    //单位属性
    public void OrgPropBind()
    {
    }

    //属性1
    public void TypeBind()
    {
    }

    public void org_characterBind()
    {
    }

    public void org_LevelBind()
    {
    }

    public void org_SortBind()
    {
    }

    public void orgTypesBind()
    {
    }


    protected void GrantBtn1_ServerClick(object sender, EventArgs e)
    {
        if (ViewState["VOrgType"].ToString().Trim() == "0" || ViewState["VOrgType"].ToString().Trim() == "")
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请选择左边的组织类别后,再点击此按钮!');window.close();</script>");
            return;
        }
        string sEditType = hidEditType.Value.ToString().Trim();
        string OID = hOrgID1.Value.ToString().Trim();
        string[] sOrgID = OID.Split(',');

        string sOrgName = MyCl .CutLimitString (DeptName1.Text.ToString().Trim());   //去掉特殊字符

        string NewOID = hNewOrgID1.Value.ToString().Trim();
        string[] sNewOrgID = NewOID.Split(',');

        string OParentID = hOldParentID1.Value.ToString().Trim();   //原上级组织ID
        string[] sOldParentID = OParentID.Split(',');

        string NParentID = hNewParentID1.Value.ToString().Trim();   //新的上级组织ID
        string[] sNewParentID = NParentID.Split(',');

        string sParentName = OrgName1.Text.ToString().Trim();
        string sOrder = MyCl.CutLimitString(DOrder.Text.ToString().Trim());
        if (sOrder.Trim() == "")
        {
            sOrder = "300";
        }

        string sPhone = MyCl.CutLimitString(Phone.Text.ToString().Trim());
        string Pcipal = HChoiceID_1.Value.ToString().Trim();  // hPersID1.Value.ToString().Trim();       //负责人



        string[] sPrincipal = Pcipal.Split(',');
        string sFax = MyCl.CutLimitString(Fax.Text.ToString().Trim());

        string sHOrgName = hOldOrgName.Value.ToString().Trim();

        string sEffective = Effective.SelectedValue.ToString().Trim();

        string sPost = Post.Text.ToString().Trim();
        string sAddress = Address.Text.ToString().Trim();
        string sWebsite = Website.Text.ToString().Trim();
        string sComWorkGroup = ComWorkGroup.Text.ToString().Trim();
        string sType = Type.SelectedValue.ToString().Trim();
        string sorg_character = org_character.SelectedValue.ToString().Trim();
        string sorg_Level = org_Level.SelectedValue.ToString().Trim();
        string sorg_Sort = org_Sort.SelectedValue.ToString().Trim();
        string sorgTypes = orgTypes.SelectedValue.ToString().Trim();
        string sIfAccount = IfAccount.SelectedValue.ToString().Trim();
        string sProp1 = Prop1.SelectedValue.ToString().Trim();  //属性1
        
        if (sOrgName == "" || sOrgName == null)  //组织名称
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('组织名称不能为空!');</script>");
            return;
        }

        if (sHOrgName != sOrgName)
        {
            int ibackOrgName = MyDPM .JudgeOrgName (sOrgName, sNewOrgID[0]);
            if (ibackOrgName >= 1)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('部门名称重复!');</script>");   //location.href='OrgTree_List.aspx';
                return;
            }
        }
       
        string sIfSavehistoryInfo = "0";
        if (IfsaveDeptHistoryInfo.Checked == true)
        {
            sIfSavehistoryInfo = "1";
        }
        if (sEditType == "2")  //修改
        {
            if (sNewParentID[0].ToString().Trim() != sOldParentID[0].ToString().Trim())
            {
                int ibackOrgName = MyDPM.JudgeOrgName(sOrgName, sNewParentID[0]);
                if (ibackOrgName >= 1)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('部门名称重复!');</script>");   //location.href='OrgTree_List.aspx';
                    return;
                }
            }
            int iBack = 0;
            iBack = MyDPM .UpOtherDeptInfo(ViewState["VOrgType"].ToString().Trim(), sHOrgName, sOrgName, sOldParentID[0], sNewParentID[0], sOrder, sPhone, sPrincipal[0], sFax, Session["Baimasoft_Role_Code"].ToString().Trim(),sPost,sAddress,sWebsite,sComWorkGroup,sType,sorg_character,sorg_Level,sorg_Sort,sorgTypes,sIfAccount, sEffective, sOrgID[0], sIfSavehistoryInfo, ChangeReason.Text.ToString().Trim(),sProp1 );

            if (iBack == 1)
            {
                MyDPM.UpOtherChildNumber (sOldParentID[0], sNewParentID[0]);                 
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改成功!');window.close();</script>");   //location.href='OrgTree_List.aspx';
                return;
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改失败!');</script>");
                return;
            }
        }
        else if (sEditType == "1")
        {
            int iback2 = 0;
            if (OID.Trim() == NewOID.Trim())
            {
                iback2 = MyDPM .SaveOtherOrgInfo (sOrgName, ViewState["VOrgType"].ToString().Trim(), sNewOrgID[0], sOrder, sPhone, sPrincipal[0], sFax, Session["Baimasoft_Role_Code"].ToString().Trim(), sPost, sAddress, sWebsite, sComWorkGroup, sType, sorg_character, sorg_Level, sorg_Sort, sorgTypes, sIfAccount, sEffective, sProp1);
            }
            else
            {
                iback2 = MyDPM.SaveOtherOrgInfo(sOrgName, ViewState["VOrgType"].ToString().Trim(), sNewParentID[0], sOrder, sPhone, sPrincipal[0], sFax, Session["Baimasoft_Role_Code"].ToString().Trim(), sPost, sAddress, sWebsite, sComWorkGroup, sType, sorg_character, sorg_Level, sorg_Sort, sorgTypes, sIfAccount, sEffective, sProp1);
            }
            if (iback2 == 1)
            {
                MyDPM.UpOtherChildNumber(sOrgID[0], sNewParentID[0]);
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('新建组织成功!');window.close();</script>");
                return;
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('新建组织失败!');</script>");
                return;
            }
        }
    }
}
